package cn.reyuxi.simple

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import cn.reyuxi.emptyview.EmptyContentView
import cn.reyuxi.emptyview.EmptyViewType
import cn.reyuxi.emptyview.R

class SimpleEmptyContentView(private val context: Context) : EmptyContentView {

    private val mViews: SparseArray<View> by lazy {
        SparseArray<View>()
    }

    override fun getView(viewType: Int): View? {
        return mViews.get(viewType) ?: kotlin.run {
            val resId = when (viewType) {
                EmptyViewType.LOADING -> R.layout.empty_loading

                else -> R.layout.empty_no_data
            }
            val loadingView: View =
                LayoutInflater.from(context).inflate(resId, null)
            mViews.put(viewType, loadingView)
            loadingView
        }
    }

    override fun onDestroy() {
        mViews.clear()
    }
}